<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class LoginController extends BaseController {

	/*
    	登录
     */	
    public function login(){
    	if(IS_POST){
            $account = $_POST['account']; // 用户名
            $password = md5($_POST['password']); // 密码
            $map0['worker_name'] = array('like','%'.$account.'%');
			$worker0 = M('worker')->where($map0)->find();
    		if(!$worker0){
                $accoun = 3;
    			echo json_encode($accoun);
    		}else{
                $map1['worker_name'] = array('like','%'.$account.'%');
                $map1['password'] = $password;
                $pwd = M('worker')->where($map1)->find();
                if(!$pwd){
                    $pass = 4;
                    echo json_encode($pass);
                }else{
                	session('worker_id',$pwd['worker_id']);
                	$status = $pwd['status'];
                	if($status=="0"){
                		$status=0;
                	}else if($status=="1"){
                		$status=1;
                	}
                    echo json_encode($status);
                }
            }
    	}else{
    		$this->display();
    	}
	}


	//初次登录设置密码
	public function reaccount(){
		if(IS_POST){
			// var_dump($_POST);die;
			$worker_id = $_SESSION['worker_id'];
			// $data=$_POST['data'];
			$data['password'] = md5($_POST['password']);
			$worker=M('worker')->where('worker_id='.$worker_id)->save($data);
			if($worker){
				//设置密码后，改变状态
				$data['status'] = 1;
				$status = M('worker')->where('worker_id='.$worker_id)->save($data);
				if($status){
					$resSta=1;
					echo json_encode($resSta);
				}else{
					$resSta=0;
					echo json_encode($resSta);
				}
			}			
		}else{
			$worker_id = $_SESSION['worker_id'];
			$this->assign('worker_id',$worker_id);
			$this->display();
		}
	}

	//修改密码
	public function change_ps(){
		if(IS_POST){
			$worker_id = session('worker_id');
			$oldps=$_POST['oldps'];
			$ps=$_POST['ps'];
			$oldps=md5($oldps);
			$ps=md5($ps);
			$m = M('worker');
			$res = $m->where('worker_id='.$worker_id)->field('password')->find();
			$res1 = implode('',$res);//转化成字符串
			if($oldps!=$res1){
				$this->error('请填写正确的旧密码！',U('login/login'),2);
			}
			if($res1==$ps){
				$this->error('请不要输入与旧密码一致的新密码！',U('login/login'),2);
			}
			$data=array('password'=>$ps);
			if(M('worker')->where('worker_id='.$worker_id)->save($data)){
				$this->success('修改成功',U('admin/admin_list1'));
			}
			else{
				$this->error('修改失败',U('login/login'));
			}
		}else{
			$worker_id = $_GET['worker_id'];
			$this->assign('worker_id',$worker_id);
			$this->display();
		}
	}

}